// 封装请求方法
import base from "./baseURL"
import {
    useUserinfoStore
} from '@/stores/userInfo'
const httpInterceptor = {
    // 拦截前触发
    invoke(options) {
        if (options.Itype === 'php') {
            options.url = base.basePHPURL + base.PhpUrl + options.url
        } else if(options.Itype !== 'php' && options.Itypes !== 1) {
            options.url = base.baseJavaURL + base.JavaUrl + options.url
        }else{
            options.url = base.baseJavaURL + base.JavaUrls + options.url
        }
        options.header = dealHeader(options)
        console.log("🚀 ~ file: request.js:15 ~ invoke ~ options.header:", options.header)
    },
}
/**
 * 请求头处理
 * @param opt 
 */
const dealHeader = (opt) => {
    const userInfo = useUserinfoStore()
    //大部分请求头都是 application/json;charset=UTF-8
    const type = ["application/json;charset=UTF-8", "application/x-www-form-urlencoded"]
    const reqParams = {}
    //所以不传 headerType 就默认 application/json;charset=UTF-8
    reqParams['content-type'] = type[opt.headerType || 0]
    if (userInfo.isLogin) {
        if (opt.Itype === 'php') {
            reqParams['token'] = userInfo.accessToken
        }
        reqParams['YF-TOKEN'] = userInfo.accessToken
        reqParams['userId'] = userInfo.userId
        reqParams['expireTime'] = userInfo.expireTime
    }
    return reqParams
}

// 拦截 request 请求
uni.addInterceptor('request', httpInterceptor)

function request(options) {
    //是否显示加载状态
    if (options.isLoad) {
        uni.showLoading({
            title: '加载中...',
            mask: true
        })
    }
    return new Promise((resolve, reject) => {
        uni.request({
            ...options,
            success: (res) => {
                console.log("🚀 ~ file: request.js:56 ~ returnnewPromise ~ res:", res)
                uni.hideLoading()
                if (res.statusCode === 200) {
                    if (res.data.code === 10000 || res.data.code === 0) {
                        resolve(res.data)
                    } else if (res.data.code === -10010) {
                        //token失效了
                        uni.setStorageSync("isLogin", false);
                        uni.reLaunch({
                        	url: '/pages/login/wxlogin'
                        })
                        //清除本地缓存
                        reject(res.data)
                    } else {
                        uni.showToast({
                            icon: 'none',
                            title: res.data.msg || '请求错误',
                        })
                        reject(res.data)
                    }
                } else {
                    uni.showToast({
                        icon: 'none',
                        title: res.data.msg || '请求错误',
                    })
                    reject(res.data)
                }
            },
            fail: (error) => {
                uni.showToast({
                    icon: 'none',
                    title: '网络错误，换个网络试试',
                })
                reject(error)
            }
        })

    })
}

// GET 请求  data 是个对象 由api.js 传递进来的
export const get = function(data) {
    return request({
        ...data,
        method: 'GET'
    })
}
// post 请求
export const post = function(data) {
    return request({
        ...data,
        method: 'POST'
    })
}
// PUT 请求 
export const put = function(data) {
    return request({
        ...data,
        method: 'PUT'
    })
}
// DEL 请求
export const del = function(data) {
    return request({
        ...data,
        method: 'DELETE'
    })
}